<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<title>后台</title>
		<link rel="stylesheet" href="/static/admin/css/style.css">
		<script src="/static/admin/js/ckeditor/ckeditor.js"></script>
	</head>
	<body>
		<div class="wrap wrap-article-edit">
			<h1>文章管理</h1>
			<div class="tips"></div>
			<div class="box">
				<div class="box-title">添加/编辑文章</div>
				<div class="box-body">
<!--					<form method="post" action="?id=2&token=d1d8da704cea56e9045df5ca761993c0" enctype="multipart/form-data">-->
						<table>
							{volist name="data" id="vo"}
							<tr>
								<th width="80">标题：</th>
								<td><input type="text" name="title" value="{$vo.title}"></td>
							</tr>
							<tr>
								<th>栏目：</th><td><select name="category_id">
								<option value="0">无</option>
								{foreach $list as $k=>$v}
									{if $vo.category_id == $v.id}     <!--判断用户的栏目是否是主栏目,是的话勾选上-->
									<option value="{$v.id}"  selected>{$v.name}</option>
									{else /}
									<option value="{$v.id}" >{$v.name}</option>
									{/if}
							<!--子栏目处理-->
									{if isset($v.child)}
										{foreach $v.child as $val} <!--如果主栏目里有子栏目就循环处理-->
									{if $vo.category_id == $val.id}
									<option value="{$val.id}"  selected>—— {$val.name}</option>
										{else /}
									<option value="{$val.id}" >—— {$val.name}</option>
										{/if}
									{/foreach}
									{/if}
								{/foreach}
								</select></td>
							</tr>
							<tr class="s-author">
								<th>作者：</th>
								<td><input type="text" name="author" value="{$vo.user.username}" disabled="disabled">
									<input type="hidden" value="{$vo.id}"  name="id">
								</td>
							</tr>
							<tr class="s-keywords">
								<th>关键字：</th>
								<td><input type="text" name="keywords" value="{$vo.keyword.keywords}">
								<span>多个关键字 请用英文逗号（,）分开</span>
								</td>
							</tr>
							<tr class="s-description">
								<th>内容提要：</th>
								<td>
									<textarea name="description">{$vo.description}</textarea>
									<span>（内容提要请在 200 个字以内）</span>
								</td>
							</tr>
			<tr class="s-thumb">
				<th>封面图片：</th>
				<td>
					<input type="file" name="thumb"><span>（超过 780*220 图片将被缩小）</span>
					<img src="{$vo.thumb}" alt="封面图" id="img" width="100px" height="100px"  >
				</td>
			</tr>
		<tr class="s-editor"><th>编辑内容：</th><td><textarea name="content">{$vo.article_detail.content}</textarea></td></tr>
	{/volist}
		<tr class="s-act"><th></th>
			<td>
			<input type="submit"  onclick="isUpdate(this)" value="提交修改">
			</td>
		</tr>
	</table>
<!--	</form>-->
	</div>
	</div>
</div>
<script src="/static/admin/js/article.config.js"></script>
<script src="/static/admin/js/jquery.2.1.4.min.js"></script>
<script>
	CKEDITOR.config.height = 400;
	CKEDITOR.config.width = "100%";
	let image = '';
	let title = $("input[name=title]");//标题
	let category_id = $("select[name=category_id]");//栏目
	let author = $("input[name=author]");//作者
	let keywords =  $("input[name=keywords]");//关键字
	let description = $("textarea[name=description]");//内容摘要
	let  content = CKEDITOR.replace("content");//编辑的内容
	let id = $("input[name=id]");
	$('[name="thumb"]').change(function (){
		let formData = new FormData();
		formData.append('thumb',$("input[name=thumb]")[0].files[0]);//图片
		$.ajax({
			type:'POST',
			url:'upload',
			data:formData,
			processData:false,
			contentType:false,
			success:function (res){
				$('[id="img"]').attr('src',res);
				image = res;
			},
		})
	})
	function isUpdate(save){
		save.value == '立即发布' ? state=1 : state=0;//发帖/草稿
		if(image == '')image = $('#img').attr('src'); //
		let formData = new FormData();
		formData.append('thumb',image);//图片
		formData.append('title',title.val());
		formData.append('category_id',category_id.val());
		formData.append('author',author.val());
		formData.append('keywords',keywords.val());
		formData.append('description',description.val());
		formData.append('content',content.getData());
		formData.append('save',state);
		formData.append('id',id.val());//更新的文章id
		$.ajax({
			url:'cp_article_edit_2',
			type:'POST',
			data:formData,
			dataType:'json',
			processData:false,
			contentType:false,
			success:function (res){
				if(res.code== 0){
					alert(res.msg);
				}else{
					alert(res.msg);
				}
			}
		})
	}
</script>
</body>
</html>